package com.acme.bankapp.domain.bank;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.LinkedList;
import java.util.Collections;

import com.acme.bankapp.service.bank.ClientRegistrationListener;

@SuppressWarnings("serial")
public class Bank implements Serializable {

	private List<Client> clients;
	private List<ClientRegistrationListener> clientRegistrationListeners;

	public Bank() {
		clients = new LinkedList<Client>();
		clientRegistrationListeners = new LinkedList<ClientRegistrationListener>();

		class DebugClientListener implements ClientRegistrationListener {
			public void onClientAdded(Client client) {
				System.out.println("DEBUG: " + (new Date()).toString()
						+ "   Client " + client.getClientSalutation()
						+ " added");
			}
		}

		class EmailClientListener implements ClientRegistrationListener {
			public void onClientAdded(Client client) {
				System.out.println("Email for " + client.getClientSalutation()
						+ " to be sent");
			}
		}

		class PrintClientListener implements ClientRegistrationListener {
			public void onClientAdded(Client client) {
				System.out.println(client.getClientSalutation()
						+ " added to bank");
			}
		}
		addClientRegistrationListener(new DebugClientListener());
		addClientRegistrationListener(new EmailClientListener());
		addClientRegistrationListener(new PrintClientListener());
	}

	public Bank(ClientRegistrationListener[] clientRegistrationListeners) {
		this();
		for (ClientRegistrationListener clientRegistrationListener : clientRegistrationListeners) {
			this.addClientRegistrationListener(clientRegistrationListener);
		}
	}

	public void addClientRegistrationListener(
			ClientRegistrationListener clientRegistrationListener) {
		clientRegistrationListeners.add(clientRegistrationListener);
	}

	private void callClientAdded(Client client) {
		for (ClientRegistrationListener clientRegistrationListener : clientRegistrationListeners) {
			clientRegistrationListener.onClientAdded(client);
		}
	}

	public void addClient(Client client) {
		clients.add(client);
		callClientAdded(client);
	}

	public List<Client> getClients() {
		return Collections.unmodifiableList(clients);
	}
}
